package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.DrawingDTO;
import com.eastfair.venueservice.dto.DrawingPageQuery;
import com.eastfair.venueservice.dto.DrawingSaveDTO;
import com.eastfair.venueservice.dto.DrawingUpdateDTO;
import com.eastfair.venueservice.entity.Drawing;
import com.eastfair.venueservice.service.DrawingService;
import com.eastfair.venueservice.vo.DrawingVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 综合图纸分类
 * </p>
 *
 * @author dq
 * @date 2022-10-27
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/drawing")
@Api(value = "Drawing", tags = "综合图纸分类")
@PreAuth(replace = "venueservice:drawing:")
public class DrawingController extends SuperController<DrawingService, Long, Drawing, DrawingPageQuery, DrawingSaveDTO, DrawingUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private DrawingService drawingService;

    @Override
    public void handlerResult(IPage<Drawing> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Drawing> drawingList = list
                .stream()
                .map((map) -> Drawing.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(drawingList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<DrawingPageQuery> params) {
        DrawingPageQuery pageQuery = params.getModel();
    }

    /**
     * 图纸列表
     *
     * @param drawingPageQuery the drawing page query
     * @return r
     */
    @PostMapping("/getDrawingList")
    public R<List<DrawingVO>> getDrawingList(@RequestBody DrawingDTO drawingPageQuery) {
        try {
            return R.success(drawingService.listDrawingVO(drawingPageQuery));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * id获取图纸详情.
     *
     * @param id the id
     * @return the by id
     */
    @GetMapping("/get/{id}")
    public R<DrawingVO> getById(@PathVariable("id") Long id) {
        log.info("getById - id获取图纸详情, id={}", id);
        return R.successDef(drawingService.getDrawingVOById(id));
    }

    /**
     * 保存图纸
     *
     * @param drawingSaveDTO the drawing save dto
     * @return r
     */
    @SysLog("保存图纸")
    @PostMapping("/saveDrawing")
    public R<Boolean> saveDrawing(@RequestBody DrawingDTO drawingSaveDTO) {
        return R.success(drawingService.saveDrawing(drawingSaveDTO));
    }

    /**
     * 修改图纸
     *
     * @param drawingUpdateDTO the drawing update dto
     * @return r
     */
    @SysLog("修改图纸")
    @PostMapping("/updateDrawing")
    public R<Boolean> updateDrawing(@RequestBody DrawingDTO drawingUpdateDTO) {
        return R.success(drawingService.updateDrawing(drawingUpdateDTO));
    }

    /**
     * 删除图纸
     *
     * @param id the id
     * @return r
     */
    @SysLog("删除图纸")
    @GetMapping("/delDrawing/{id}")
    public R<Boolean> delDrawing(@PathVariable("id") Long id) {
        return R.success(drawingService.delDrawing(id));
    }
}
